<?php

class pdb extends Model 
{

	var $pgdata;

	
	function pdb()
	{     
		parent::Model();
		log_message('debug', 'PzzAzzDB: pdb library initialized');
	}
	
	/*********************************************
	* Get user data
	*
	* @param string $data , from Auth or Account db fields
	* @return object
	*
	* $udata = $this->db->getUserData('City', 'State');
	*/	
	function getUserData($data)
	{
		$uid = $this->session->userdata('uid');
		$this->db->select($data);
		$this->db->from('Auth');
		$this->db->join('Accounts', 'Auth.uid = Accounts.uid');
		$this->db->where('uid', $uid);
		$result = $this->db->get();
		return $result;
	}
	
	/*********************************************
	* Get alias and password
	*
	* @param varchar $alias
	* @param varchar $pass
	* @return object
	*/
	function Login($a, $p)
	{
		$this->db->select('uid,Alias,dgroup,groups');
		$this->db->from('Auth');
		$this->db->where('Alias', $a);
		$this->db->where('Pass', $p);
		$this->db->limit(1);
		$query = $this->db->get();
		if ($query->num_rows() > 0)
		{
			$row = $query->row(); 
			$sql = "UPDATE `Auth` SET `Count`=`Count`+1 WHERE `uid`= $row->uid";
			$this->db->query($sql);
			return $row;
		}else{
			return $data = FALSE;
		}
	}
	
	/*********************************************
	* Get cookie
	*
	*
	* @return object
	*/
	function getCookie()
	{
		return $data = $this->getUserData('cookie');
	}
	
	
	/*********************************************
	* Get eMail, checks if the email address is already in our db
	* 
	* @param varchar $email
	* @return array
	*/
	function Email($email)
	{
		return $data = $this->getUserData('eMail');
	}
	
	/*********************************************
	* Register/Add new user in to db
	*
	* @param varchar $alias
	* @param varchar $email
	* @param varchar $timezone
	* @param varchar $pass
	* @return array
	*/
	function Register($alias, $email, $timezone)
	{	
		$data = array(
			'Alias' => $alias ,
			'Pass' => $this->fun->randString(),
			'IP' => $this->fun->getip()
			);
		$this->db->insert('Auth', $data);
		
		$uid = $this->db->insert_id();
		$data = array(
			'uid' => $uid,
			'newemail' => $email ,
			'GMT' => $timezone,
			'IP' => $this->fun->getip(),
			'date_joined' => 'NOW()'
			);
		$this->db->insert('Account', $data);
		
      return $data;
	}
	
	function dbGet($cat,$name,$sub,$value = FALSE)
	{
		//$data = $this->prepData($cat,$name,$sub,$value,$ext = FALSE);
		if($value){
			$data = array(
				'cat' 	=> "'$cat'",
				'name' 	=> "'$name'",
				'sub' 	=> "'$sub'",
				'value' 	=> "'$value'"
				);
		}else{
			$data = array(
				'cat' 	=> "'$cat'",
				'name' 	=> "'$name'",
				'sub' 	=> "'$sub'"
				);
		}
		$where  = '';
		foreach($data as $v => $k)
		{
			$where  .= "`$v` = $k AND";
		}
		
		$where  = $this->RLComma($where);

		$sql = "SELECT * FROM `data` WHERE $where";
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0)
		{
			return  $query->result();
		}
		else
		{
			return FALSE;
		}
	}

	function dbPut($cat,$name,$sub,$value,$ext = FALSE)
	{
		//$data = $this->prepData($cat,$name,$sub,$value,$ext = FALSE);
		
		$select = $this->dbGet($cat,$name,$sub,$value);

		//$value = $this->db->escape($value);		
		if ($select !== FALSE) // Update
		{
			if($ext)
			{
				$set = "`ext` = '$ext'";
			}
			else
			{
				$set = "`value`= '$value'";
			}
			$id = $select[0]->id;
			$sql = "UPDATE `data` SET $set WHERE `id` = $id";
			$query = $this->db->query($sql); 
		}
		else // Insert
		{
			if($ext)
			{
				$sql = "INSERT INTO `data` (`cat`, `name`, `sub`, `value`,`ext`) 
						VALUES ('$cat', '$name', '$sub', '$value', '$ext')";
			}
			else
			{
				$sql = "INSERT INTO `data` (`cat`, `name`, `sub`, `value`) 
						VALUES ('$cat', $name, '$sub', $value)";
			}
			$query = $this->db->query($sql);
		}
	}
	
	function prepData($cat,$name,$sub,$value,$ext = FALSE)
	{
		$data = array(
			'cat' 	=> $cat,
			'name' 	=> $name,
			'sub' 	=> $sub,
			'value' 	=> $this->db->escape($value)
			);
		if($ext){
			$data['ext'] = $ext;
		}
		return $data;
	}
	
	function RLComma($string)
	{
		//here we remove the last comma.
		$string = trim($string);
		$last = $string{strlen($string)-1};
		if (!strcmp($last,",")){
			$string = rtrim($string, ',');
			$string = trim($string);
		}
		log_message('debug', 'PzzAzzDB: string: ' . $string);
		$last = $string{strlen($string)-1};
		log_message('debug', 'PzzAzzDB: last' . $last);
		if (!strcmp($last,"D")){
			$string = rtrim($string, 'AND');
			log_message('debug', 'PzzAzzDB: string' . $string);
			$string = trim($string);
		}

		return $string;
	}
}
?>
